package class41To50;

import java.io.*;
import java.util.Arrays;


//https://www.nowcoder.com/practice/7f8a8553ddbf4eaab749ec988726702b
public class TrieTree {

    public static final int MAX_N = 150005;

    public static int[][] tree = new int[MAX_N][26];

    public static int[] pass = new int[MAX_N];

    public static int[] end = new int[MAX_N];

    public static int cnt;


    public static void insert(String word) {
        int cur = 1;
        pass[cur]++;
        for (int i = 0; i < word.length(); i++) {
            int index = word.charAt(i) - 'a';
            if (tree[cur][index] == 0) {
                tree[cur][index] = ++cnt;
            }
            cur = tree[cur][index];
            pass[cur]++;
        }
        end[cur]++;
    }

    public static void delete(String word) {
        if (search(word) == 0) return;
        int cur = 1;
        for (int i = 0; i < word.length(); i++) {
            int index = word.charAt(i) - 'a';
            if (--pass[tree[cur][index]] == 0) {
                tree[cur][index] = 0;
                return;
            }
            cur = tree[cur][index];
        }
        end[cur]--;
    }

    public static int search(String word) {
        int cur = 1;
        for (int i = 0; i < word.length(); i++) {
            int index = word.charAt(i) - 'a';
            if (tree[cur][index] == 0) {
                return 0;
            }
            cur = tree[cur][index];
        }
        return end[cur];
    }

    public static int startWith(String prefix) {
        int cur = 1;
        for (int i = 0; i < prefix.length(); i++) {
            int index = prefix.charAt(i) - 'a';
            if (tree[cur][index] == 0) {
                return 0;
            }
            cur = tree[cur][index];
        }
        return pass[cur];
    }

    public static void clear() {
        for (int i = 0; i <= cnt; i++) {
            Arrays.fill(tree[i], 0);
            pass[i] = 0;
            end[i] = 0;
        }
    }


    public static int m;

    public static int op;

    public static String[] splits;

    public static void build() {
        cnt = 1;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        String line = null;
        while ((line = in.readLine()) != null) {
            build();
            m = Integer.valueOf(line);
            for (int i = 0; i < m; i++) {
                splits = in.readLine().split(" ");
                op = Integer.valueOf(splits[0]);
                if (op == 1) {
                    insert(splits[1]);
                } else if (op == 2) {
                    delete(splits[1]);
                } else if (op == 3) {
                    out.println(search(splits[1]) > 0 ? "YES" : "NO");
                } else {
                    out.println(startWith(splits[1]));
                }
            }
            clear();
        }
        out.flush();
        in.close();
        out.close();
    }
}
